import 'package:flutter_test/flutter_test.dart';
import 'package:awak/common/models/topic.dart';
import 'package:awak/common/models/content.dart';
import 'package:awak/page/community/topic_data_holder.dart';

void main() {
  group('Topic Navigation Tests', () {
    late List<Topic> testTopics;
    late List<Content> testContents;

    setUp(() {
      // 重置状态
      TopicDataHolder.setCurrentTopic(null);
      TopicDataHolder.allTopics.clear();

      // 创建测试主题
      testTopics = [
        Topic(
          id: 1,
          title: 'Philosophy and Deep Thinking',
          label: 'Philosophy',
          content: 'Explore the depths of philosophical thought',
          coverImage: 'philosophy.jpg',
          numberPeople: 150,
          userId: 1,
        ),
        Topic(
          id: 2,
          title: 'Travel Adventures Around the World',
          label: 'Travel',
          content: 'Share your travel experiences',
          coverImage: 'travel.jpg',
          numberPeople: 200,
          userId: 2,
        ),
        Topic(
          id: 3,
          title: 'Very Long Topic Title That Should Wrap to Multiple Lines When Displayed in the UI',
          label: 'Long Title',
          content: 'Testing long titles',
          coverImage: 'long.jpg',
          numberPeople: 50,
          userId: 3,
        ),
      ];

      // 创建测试内容
      testContents = [
        Content(
          id: 1,
          picture: 'content1.jpg',
          description: 'Philosophical content about existence',
          userId: 1,
          topicId: 1,
          publishedAt: DateTime.now().subtract(Duration(hours: 1)),
        ),
        Content(
          id: 2,
          picture: 'content2.jpg',
          description: 'Travel story from Japan',
          userId: 2,
          topicId: 2,
          publishedAt: DateTime.now().subtract(Duration(hours: 2)),
        ),
        Content(
          id: 3,
          picture: 'content3.jpg',
          description: 'Content with very long topic title',
          userId: 3,
          topicId: 3,
          publishedAt: DateTime.now().subtract(Duration(hours: 3)),
        ),
      ];

      TopicDataHolder.allTopics = testTopics;
    });

    test('should handle long topic titles gracefully', () {
      final longTitleTopic = testTopics[2];
      
      expect(longTitleTopic.title.length, greaterThan(50));
      expect(longTitleTopic.title, contains('Very Long Topic Title'));
      expect(longTitleTopic.title, contains('Multiple Lines'));
      
      // 验证标题包含完整信息
      expect(longTitleTopic.title, 'Very Long Topic Title That Should Wrap to Multiple Lines When Displayed in the UI');
    });

    test('TopicDataHolder should manage current topic for navigation', () {
      final topic = testTopics[0];
      
      // 测试设置当前主题
      TopicDataHolder.setCurrentTopic(topic);
      expect(TopicDataHolder.currentTopic, topic);
      expect(TopicDataHolder.currentTopic?.id, 1);
      expect(TopicDataHolder.currentTopic?.title, 'Philosophy and Deep Thinking');

      // 测试清除当前主题
      TopicDataHolder.setCurrentTopic(null);
      expect(TopicDataHolder.currentTopic, null);

      // 测试根据ID获取主题
      final foundTopic = TopicDataHolder.getTopicById(1);
      expect(foundTopic, topic);
      expect(foundTopic?.label, 'Philosophy');
    });

    test('should associate content with correct topic for navigation', () {
      // 验证内容与主题的关联
      for (final content in testContents) {
        final topic = testTopics.where((t) => t.id == content.topicId).firstOrNull;
        expect(topic, isNotNull);
        
        if (content.topicId == 1) {
          expect(topic?.label, 'Philosophy');
          expect(topic?.title, contains('Philosophy'));
        } else if (content.topicId == 2) {
          expect(topic?.label, 'Travel');
          expect(topic?.title, contains('Travel'));
        } else if (content.topicId == 3) {
          expect(topic?.label, 'Long Title');
          expect(topic?.title, contains('Very Long'));
        }
      }
    });

    test('should support topic navigation flow from content', () {
      // 模拟从content点击主题标签的流程
      final content = testContents[0]; // Philosophy content
      final topic = testTopics.where((t) => t.id == content.topicId).first;
      
      // 1. 用户点击content的主题标签
      TopicDataHolder.setCurrentTopic(topic);
      expect(TopicDataHolder.currentTopic, isNotNull);
      
      // 2. 验证主题数据完整性
      final currentTopic = TopicDataHolder.currentTopic!;
      expect(currentTopic.title.isNotEmpty, true);
      expect(currentTopic.label.isNotEmpty, true);
      expect(currentTopic.content.isNotEmpty, true);
      expect(currentTopic.numberPeople, greaterThan(0));
      
      // 3. 验证可以获取该主题的相关内容
      final topicContents = testContents.where((c) => c.topicId == currentTopic.id).toList();
      expect(topicContents.isNotEmpty, true);
      expect(topicContents.first.topicId, currentTopic.id);
    });

    test('should handle topic tag click navigation', () {
      // 测试主题标签点击导航逻辑
      final topic = testTopics[1]; // Travel topic
      
      // 模拟点击主题标签
      TopicDataHolder.setCurrentTopic(topic);
      
      // 验证导航状态
      expect(TopicDataHolder.currentTopic?.id, 2);
      expect(TopicDataHolder.currentTopic?.label, 'Travel');
      expect(TopicDataHolder.currentTopic?.title, contains('Travel Adventures'));
      
      // 验证可以获取更新的主题数据
      final updatedTopic = TopicDataHolder.getTopicById(topic.id!) ?? topic;
      expect(updatedTopic.id, topic.id);
      expect(updatedTopic.numberPeople, topic.numberPeople);
    });

    test('should maintain data consistency during navigation', () {
      // 验证导航过程中的数据一致性
      final originalTopic = testTopics[0];
      
      // 设置当前主题
      TopicDataHolder.setCurrentTopic(originalTopic);
      final currentTopic = TopicDataHolder.currentTopic!;
      
      // 验证数据一致性
      expect(currentTopic.id, originalTopic.id);
      expect(currentTopic.title, originalTopic.title);
      expect(currentTopic.label, originalTopic.label);
      expect(currentTopic.content, originalTopic.content);
      expect(currentTopic.numberPeople, originalTopic.numberPeople);
      
      // 验证通过ID获取的主题与当前主题一致
      final retrievedTopic = TopicDataHolder.getTopicById(currentTopic.id!);
      expect(retrievedTopic?.id, currentTopic.id);
      expect(retrievedTopic?.title, currentTopic.title);
    });

    test('should handle multiple topic navigation scenarios', () {
      // 测试多个主题导航场景
      
      // 场景1：从推荐主题导航
      final recommendedTopic = testTopics[0];
      TopicDataHolder.setCurrentTopic(recommendedTopic);
      expect(TopicDataHolder.currentTopic?.label, 'Philosophy');
      
      // 场景2：从content主题标签导航
      final contentTopic = testTopics[1];
      TopicDataHolder.setCurrentTopic(contentTopic);
      expect(TopicDataHolder.currentTopic?.label, 'Travel');
      
      // 场景3：导航到长标题主题
      final longTitleTopic = testTopics[2];
      TopicDataHolder.setCurrentTopic(longTitleTopic);
      expect(TopicDataHolder.currentTopic?.title.length, greaterThan(50));
      
      // 验证最后设置的主题是当前主题
      expect(TopicDataHolder.currentTopic?.id, 3);
      expect(TopicDataHolder.currentTopic?.label, 'Long Title');
    });

    test('should support content-to-topic relationship mapping', () {
      // 验证内容到主题的关系映射
      final contentTopicMap = <int, Topic>{};
      
      for (final content in testContents) {
        final topic = testTopics.where((t) => t.id == content.topicId).firstOrNull;
        if (topic != null) {
          contentTopicMap[content.id!] = topic;
        }
      }
      
      // 验证映射完整性
      expect(contentTopicMap.length, testContents.length);
      
      // 验证具体映射关系
      expect(contentTopicMap[1]?.label, 'Philosophy');
      expect(contentTopicMap[2]?.label, 'Travel');
      expect(contentTopicMap[3]?.label, 'Long Title');
      
      // 验证可以通过content找到对应的topic
      final philosophyContent = testContents[0];
      final philosophyTopic = contentTopicMap[philosophyContent.id!];
      expect(philosophyTopic?.title, contains('Philosophy'));
    });

    test('should handle edge cases in topic navigation', () {
      // 测试边界情况

      // 1. 不存在的主题ID
      expect(TopicDataHolder.getTopicById(999), null);

      // 2. 设置null主题
      TopicDataHolder.setCurrentTopic(null);
      expect(TopicDataHolder.currentTopic, null);

      // 3. 重复设置相同主题
      final topic = testTopics[0];
      TopicDataHolder.setCurrentTopic(topic);
      TopicDataHolder.setCurrentTopic(topic);
      expect(TopicDataHolder.currentTopic?.id, topic.id);

      // 4. 空主题列表（最后测试，避免影响其他测试）
      final originalTopics = List<Topic>.from(TopicDataHolder.allTopics);
      TopicDataHolder.allTopics.clear();
      expect(TopicDataHolder.getTopicById(1), null);
      TopicDataHolder.allTopics = originalTopics; // 恢复数据
    });

    test('should validate topic title text wrapping requirements', () {
      // 验证主题标题文本换行需求
      final longTitleTopic = testTopics[2];
      
      // 验证长标题的特征
      expect(longTitleTopic.title.length, greaterThan(50));
      expect(longTitleTopic.title.split(' ').length, greaterThan(10));
      
      // 验证标题包含多个单词，适合换行
      final words = longTitleTopic.title.split(' ');
      expect(words.length, greaterThanOrEqualTo(10));
      
      // 验证标题没有特殊字符会影响换行
      expect(longTitleTopic.title, isNot(contains('\n')));
      expect(longTitleTopic.title, isNot(contains('\t')));
      
      // 验证标题是连续的文本，适合自动换行
      expect(longTitleTopic.title.trim(), longTitleTopic.title);
    });
  });
}
